//大数相加
//"0009007199254740991"
//"1234567899999999999"

let a = '9007199254740991'
let b = '1234567899999999999'
function add(a, b) {
    //取两个数字的最大长度
    let maxLength = Math.max(a.length, b.length);
    //用0去补齐长度
    a = a.padStart(maxLength, 0);//"0009007199254740991"
    b = b.padStart(maxLength, 0);//"1234567899999999999"
    //定义加法过程中需要用到的变量
    let t = 0;
    let f = 0;   //"进位"
    let sum = "";
    for (let i = maxLength - 1; i >= 0; i--) {
        t = parseInt(a[i]) + parseInt(b[i]) + f;
        f = Math.floor(t / 10);
        sum = t % 10 + sum;
    }
    if (f !== 0) {
        sum = '' + f + sum;
    }
    return sum;
}


function add1(a, b) {
    let maxLength = Math.max(a.length, b.length)

    a = a.padStart(maxLength, 0)
    b = b.padStart(maxLength, 0)

    //从大往小加
    let sign = 0 //进位
    let sum = '' //和
    for (let i = maxLength - 1; i >= 0; i--) {
        let temp = Number(a[i]) + Number(b[i]) + sign
        sign = Math.floor(temp / 10)
        sum = temp % 10 + sum
    }
    //进位是否大于0
    if (sign > 0) {
        sum = sign + sum
    }
    return sum
}

console.log(add(a, b))
console.log(add1(a, b))
